//
//  StatuesViewModel.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/5/3.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit

class StatuesViewModel: NSObject {

    var status: Statues?
     // MARK:- #注释>用户数据处理
    var sourceText :String?  //处理过后的链接文本
    var createTime : String?  //处理过后的时间
    var verifiedImage : UIImage?   //处理认证信息
    var vipImage :UIImage?   //处理等级信息
    
    var headImageUrl : URL? //处理用户头像url
    
    var picURLs :[URL] = [URL]()  //处理微博配图url
    
    
    
    init(status:Statues) {
        self.status = status
        //对来源处理
        if let source = status.source,source != "" {
            
            // MARK:- #注释>字符串截取
            let startIndex = (source as NSString).range(of: ">").location+1  //获取起始位置
            let length = (source as NSString).range(of: "</").location - startIndex
            
            //字符串截取
            sourceText = (source as NSString).substring(with: NSRange(location: startIndex, length: length))

        }
        //处理时间
        if let createStrTime = status.created_at{
            
             createTime = Date.createDateString(createdTime: createStrTime)
        }
        //处理认证
        switch status.user?.verified_type ?? -1{
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //处理会员图标
        if status.user!.mbrank > 0 && status.user!.mbrank < 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        
        let profile_image_url = status.user?.profile_image_url ?? ""
        headImageUrl = URL(string: profile_image_url)
        
        //处理配图数据
        
        let picURLDicts = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        
        
        if let picURLDicts = picURLDicts {
            for picURLDict in picURLDicts {
                
                guard let picUrlString = picURLDict["thumbnail_pic"] else{
                    
                    continue
                }
                picURLs.append(URL(string: picUrlString)!)
                
                
            }
        }
        
    }
    
    
    
}
